<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="java.io.BufferedReader" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.io.InputStreamReader" %>
<%@ page import="java.io.PrintWriter" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.net.URLConnection" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<HTML>
<HEAD>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style type="text/css">
        body {
            font-family: 'Open Sans', sans-serif;
            font-weight: 300;
            line-height: 1.42em;
            color: #111a0e;
            background-color: rgba(176, 239, 251, 0);
        }
        .container td {
            font-weight: normal;
            font-size: 1em;
            -webkit-box-shadow: 0 2px 2px -2px #0E1119;
            -moz-box-shadow: 0 2px 2px -2px #0E1119;
            box-shadow: 0 2px 2px -2px #0E1119;
        }
        .container {
            text-align: center;
            overflow: hidden;
            width: 100%;
            height: 100%;
            margin: 0 auto;
            display: table;

        }
        .container td {
            width: 50px;
            height: 65px;
            padding-bottom: 0%;
            padding-top: 2%;
            padding-left:2%;
        }
        /* Background-color of the odd rows */
        .container tr:nth-child(odd) {
            background-color: #b0effb;
        }
        /* Background-color of the even rows */
        .container tr:nth-child(even) {
            background-color: #b0effb;
        }
        .container td:first-child { color: #111a0e; }
        .container td:hover {
            background-color: #cbe3ff;
            color: #403E10;
            font-weight: bold;
            box-shadow: #cccfff -1px 1px, #cccfff -2px 2px, #cccfff -3px 3px, #cccfff -4px 4px, #cccfff -5px 5px, #cccfff -6px 6px;
            transform: translate3d(6px, -6px, 0);
            transition-delay: 0s;
            transition-duration: 0.4s;
            transition-property: all;
            transition-timing-function: line;
        }
        @media (max-width: 800px) {
            .container td:nth-child(4),
            .container th:nth-child(4) { display: none; }
        }
    </style>
    <%!
        public static JSONObject sendPostHttpRequest(String url, String param) {
            PrintWriter out = null;
            BufferedReader in = null;
            String result = "";
            JSONObject jsonResult = null;
            try {
                URL realUrl = new URL( url );
                URLConnection connection = realUrl.openConnection();
                // 设置通用的请求属性
                connection.setRequestProperty( "accept", "*/*" );
                connection.setRequestProperty( "connection", "Keep-Alive" );
                connection.setRequestProperty( "user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)" );
                connection.setRequestProperty( "Content-Type", "application/json; charset=utf-8" );
                // 发送POST请求必须设置如下两行
                connection.setDoOutput( true );
                connection.setDoInput( true );
                //1.获取URLConnection对象对应的输出流
                out = new PrintWriter( connection.getOutputStream() );
                //2.中文有乱码的需要将PrintWriter改为如下
                //out=new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
                // 发送请求参数
                out.print( param );
                // flush输出流的缓冲
                out.flush();
                // 定义BufferedReader输入流来读取URL的响应
                in = new BufferedReader( new InputStreamReader( connection.getInputStream(), "UTF-8" ) );
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
                jsonResult = JSONObject.parseObject( result );
            } catch (Exception e) {
                System.out.println( "发送 POST 请求出现异常！" + e );
                e.printStackTrace();
            }
            //使用finally块来关闭输出流、输入流
            finally {
                try {
                    if (out != null) {
                        out.close();
                    }
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return jsonResult;
        }
    %>
    <%
        User user = HrmUserVarify.getUser( request, response );
        String loginid = user.getLoginid();
        JSONObject json = new JSONObject();
        json.put( "empCode", "1003918" );
        String param = json.toJSONString();
        JSONArray list = new JSONArray();
        String URL = "http://192.168.1.102:9090/com.cese2.sso.apps.all.login";
        JSONObject result = sendPostHttpRequest( URL, param );
        JSONObject res = (JSONObject) result.get( "response" );
        JSONArray dataList = (JSONArray) res.get( "dataList" );
        for (int i = 0; i < dataList.size(); i++) {
            JSONObject data = new JSONObject();
            String appName = (String) dataList.getJSONObject( i ).get( "appName" );
            String url = (String) dataList.getJSONObject( i ).get( "url" );
            data.put( "appName", appName );
            data.put( "url", url );
            list.add( data );
        }
    %>
</HEAD>
<body>
<table class="container">
    <%for(int i=0;i<list.size();i++) {%>
    <%if(i%3==0){%>
    <tr>
        <td onclick="javascript:window.open('<%=list.getJSONObject( i ).getString( "url" )%>');">
            <%=list.getJSONObject(i).getString("appName")%>
        </td>
    <%}%>
    <%if(i%3==1){%>
        <td onclick="javascript:window.open('<%=list.getJSONObject( i ).getString( "url" )%>');">
            <%=list.getJSONObject(i).getString("appName")%>
        </td>
    <%}%>
     <%if(i%3==2){%>
        <td onclick="javascript:window.open('<%=list.getJSONObject( i ).getString( "url" )%>');">
            <%=list.getJSONObject(i).getString("appName")%>
        </td>
     <%}%>


        <%}%>

    <%if(list.size()%3==0){%>
    </tr>
    <%}%>
    <%if(list.size()%3==1){%>
     <td></td>
     <td></td>
    </tr>
    <%}%>
    <%if(list.size()%3==2){%>
    <td></td>
    </tr>
    <%}%>
</table>
</body>
</HTML>